package com.itfreer.power.dao.utils;

import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

/**
 * 定义查询参数转换类
 */
public class PredicateConvertUtils {

	@SuppressWarnings("unchecked")
	public DBObject getMapWhereObject(Map<String, Object> wheremap) {
		if (wheremap == null) {
			return new BasicDBObject();
		}
		DBObject queryCondition = new BasicDBObject();
		Set<String> keys = wheremap.keySet();
		for (String key : keys) {
			String[] fields = key.split(":");
			String field = null;
			String option = null;
			if (fields.length == 2) {
				field = fields[0];
				option = fields[1];
			} else {
				field = key;
				option = "=";
			}
			// 如果是or操作符
			if (field.trim().toLowerCase().equals("or")) {
				Object value = wheremap.get(key);
				Map<String, Object> orWhereMap = null;
				if (value instanceof Map) {
					orWhereMap = (Map<String, Object>) value;
				}
				DBObject innerQueryCondition = getDbWhere(orWhereMap, key, field, option);
				if (innerQueryCondition != null) {
					queryCondition.put("$or", innerQueryCondition);
				}
			} else {
				DBObject innerQueryCondition = getDbWhere(wheremap, key, field, option);
				if (innerQueryCondition != null) {
					queryCondition.putAll(innerQueryCondition);
				}
			}
		}
		return queryCondition;
	}

	private DBObject getDbWhere(Map<String, Object> wheremap, String key,
			String field, String option) {
		DBObject queryCondition = new BasicDBObject();
		Object value = wheremap.get(key);
		if (option.equals("=")) {
			if (value == null || value.equals("")) {
				return null;
			}
			if (value.equals("\"null\"")) {
				queryCondition.put(field, null);
			} else {
				queryCondition.put(field, value);
			}
		} else if (option.equals(">")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBObject v = new BasicDBObject("$gt", value);
			queryCondition.put(field, v);
		} else if (option.equals("!=")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBObject v = new BasicDBObject("$ne", value);
			queryCondition.put(field, v);
		} else if (option.equals(">=")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBObject v = new BasicDBObject("$gte", value);
			queryCondition.put(field, v);
		} else if (option.equals("<")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBObject v = new BasicDBObject("$lt", value);
			queryCondition.put(field, v);
		} else if (option.equals("<=")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBObject v = new BasicDBObject("$lte", value);
			queryCondition.put(field, v);
		} else if (option.equals("in")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBList values = new BasicDBList();
			String[] valueStrings = value.toString().split(",");
			for (String item : valueStrings) {
				String itemValue = item;
				itemValue = itemValue.trim();
				if (itemValue.startsWith("'") && itemValue.endsWith("'")) {
					itemValue = itemValue.substring(1);
					itemValue = itemValue.substring(0, itemValue.length() - 1);
				}
				values.add(itemValue);
			}
			queryCondition.put(field, new BasicDBObject("$in", values));
		} else if (option.equals("nin")) {
			if (value == null || value.equals("")) {
				return null;
			}
			BasicDBList values = new BasicDBList();
			String[] valueStrings = value.toString().split(",");
			for (String item : valueStrings) {
				String itemValue = item;
				itemValue = itemValue.trim();
				if (itemValue.startsWith("'") && itemValue.endsWith("'")) {
					itemValue = itemValue.substring(1);
					itemValue = itemValue.substring(0, itemValue.length() - 1);
				}
				values.add(itemValue);
			}
			queryCondition.put(field, new BasicDBObject("$nin", values));
		} else if (option.equals("isnull")) {
			queryCondition.put(field, null);
		} else if (option.equals("isnnull")) {
			BasicDBObject v = new BasicDBObject("$ne", null);
			queryCondition.put(field, v);
		} else if (option.equals("like")) {
			if (value == null || value.equals("")) {
				return null;
			}
			Pattern pattern = Pattern.compile("^.*" + value.toString() + ".*$",
					Pattern.CASE_INSENSITIVE);
			queryCondition.put(field, pattern);
		} else {
			throw new RuntimeException("不支持的操作符【" + option + "】！");
		}
		return queryCondition;
	}
	
}
